package com.example.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.zookeeper.Testable;

import java.util.Comparator;
import java.util.StringTokenizer;

import java.io.IOException;

//用于将第一阶段输出的按字典序排列的数据以值的形式排列
public class MapReduceDriver2 {
    public static class StockMapper2 extends Mapper<Object, Text, IntWritable, Text> {
        private final IntWritable countAsKey = new IntWritable();
        private final Text stockCodeAsValue=new Text();
        @Override
        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
//            String[] line=value.toString().split(" ");
            String[] line=value.toString().split("\\s+");
            if(line.length>=2){
                countAsKey.set(Integer.parseInt(line[1]));
                stockCodeAsValue.set(line[0]);
                context.write(countAsKey,stockCodeAsValue);
            }
        }
    }

    public static class StockReducer2 extends Reducer< IntWritable,Text, Text, IntWritable> {
        private final Text stockCode=new Text();
        private final IntWritable count=new IntWritable();
        @Override
        protected void reduce(IntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            for (Text value : values) {
                context.write(value, key); // 交换键和值的位置
            }
        }
    }

    public static class DescSort extends  WritableComparator{
        public DescSort() {
            super(IntWritable.class,true);//注册排序组件
        }
        @Override
        public int compare(byte[] arg0, int arg1, int arg2, byte[] arg3,
                           int arg4, int arg5) {
            return -super.compare(arg0, arg1, arg2, arg3, arg4, arg5);//注意使用负号来完成降序
        }
        @Override
        public int compare(Object a, Object b) {
            return   -super.compare(a, b);//注意使用负号来完成降序
        }
    }



    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "Stock count stage2");

        job.setJarByClass(MapReduceDriver2.class);
        job.setMapperClass(StockMapper2.class);
        job.setReducerClass(StockReducer2.class);

        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(Text.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        job.setSortComparatorClass(DescSort.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

}

